package com.owc.common.configuration;

import com.owc.common.context.ActiveProfile;
import com.owc.common.Const.HeaderConst;
import io.swagger.annotations.Api;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author xiaoming
 */
@Configuration
@EnableSwagger2
public class Swagger2Configuration {

    @Bean
    public Docket swaggerSpringMvcPlugin() {

        ParameterBuilder ticketPar = new ParameterBuilder();
        ParameterBuilder ticketParDev = new ParameterBuilder();
        List<Parameter> pars = new ArrayList<>();
        /*
         * setting request params no need decrypt
         */
        ticketPar.name(HeaderConst.decrypt)
                .modelRef(new ModelRef("Boolean"))
                .parameterType("header")
                .defaultValue("false")
                .required(false)
                .build();

        /*
         * Specifying the request is a development environment
         */
        ticketParDev.name(HeaderConst.dev)
                .modelRef(new ModelRef("Boolean"))
                .parameterType("header")
                .defaultValue("true")
                .required(false)
                .build();
        pars.add(ticketPar.build());
        pars.add(ticketParDev.build());

        return new Docket(DocumentationType.SWAGGER_2)
                .useDefaultResponseMessages(false)
                .apiInfo(apiInfo())
                .globalOperationParameters(pars)
                .select()
                .apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
                .paths(PathSelectors.any())
                .build()
                .enable(!ActiveProfile.isProd());
    }


    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                // 页面标题1
                .title("OWC API INTERFACE ADDRESS")
                // 描述
                .description("OWC-API").termsOfServiceUrl("http://localhost:9090/")
                // 版本号
                .version("0.0.1").build();
    }
}
